/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.msvctch.msvc2008;

import org.eclipse.cdt.managedbuilder.core.IBuildPathResolver;
import org.eclipse.cdt.managedbuilder.core.IConfiguration;
import org.eclipse.cdt.managedbuilder.core.IEnvVarBuildPath;
import org.eclipse.jface.preference.IPreferenceStore;

import com.funkyroach.msvctch.plugin.MSVCPlugin;

public class MSVC2008PathResolver implements IBuildPathResolver {

	private static final String MSVC_HOME = "msvc_home";
	private static final String MSSDK_HOME = "mssdk_home";
	private static final String DXSDK_HOME = "dxsdk_home";


	static final String PROPERTY_OS_NAME = "os.name"; //$NON-NLS-1$
	static final String PROPERTY_OS_VALUE = "windows";//$NON-NLS-1$

	static final String DELIMITER_WIN = ";"; //$NON-NLS-1$

	public static final String DEFAULT_VISUAL_STUDIO_9_0_VC_HOME = "C:\\Program Files\\Microsoft Visual Studio 9.0\\VC";
	public static final String DEFAULT_MS_SDK_HOME = "C:\\Program Files\\Microsoft SDKs\\Windows\\v6.0A";
	public static final String DEFAULT_DX_SDK_HOME = "C:\\Program Files\\Microsoft DirectX SDK (November 2007)";
  
	static boolean checked = false;
	static String msvcHome = null;
	static String msSDKHome = null;
	static String msDXSDKHome = null;

	public String[] resolveBuildPaths(int pathType, String variableName,
			String variableValue, IConfiguration configuration) {
		StringBuffer result = new StringBuffer();

		switch (pathType) {

		case IEnvVarBuildPath.BUILDPATH_INCLUDE:
			result.append(variableValue);
			break;

		case IEnvVarBuildPath.BUILDPATH_LIBRARY:
			if ("VCINSTALLDIR".equals(variableName)) {
				result.append(variableValue + "\\lib;");
			}
		default:
			return new String[] {};
		}

		return result.toString().split(DELIMITER_WIN);
	}

	private static synchronized void checkPreferenceStore() {
		checked = true;
		
		IPreferenceStore store = MSVCPlugin.getDefault().getPreferenceStore();
		if (store.contains(MSVC_HOME)) {
			msvcHome = store.getString(MSVC_HOME);
		} else {
			msvcHome = DEFAULT_VISUAL_STUDIO_9_0_VC_HOME;
		}

		if (store.contains(MSSDK_HOME)) {
			msSDKHome = store.getString(MSSDK_HOME);
		} else {
			msSDKHome = DEFAULT_MS_SDK_HOME;
		}

		if (store.contains(DXSDK_HOME)) {
			msDXSDKHome = store.getString(DXSDK_HOME);
		} else {
			msDXSDKHome = DEFAULT_DX_SDK_HOME;
		}

	}

	public static String getMsvcHome() {
		if (!checked)
			checkPreferenceStore();
		return msvcHome;
	}

	public static void setMsvcHome(String value) {
		msvcHome = value;
		IPreferenceStore store = MSVCPlugin.getDefault().getPreferenceStore();
		store.setValue(MSVC_HOME, value);
	}

	public static String getMsSDKHome() {
		if (!checked)
			checkPreferenceStore();
		return msSDKHome;
	}

	public static void setMsSDKHome(String value) {
		msSDKHome = value;
		IPreferenceStore store = MSVCPlugin.getDefault().getPreferenceStore();
		store.setValue(MSSDK_HOME, value);
	}


	public static String getDXSDKHome() {
		if (!checked)
			checkPreferenceStore();
		return msDXSDKHome;
	}

	public static void setDXSDKHome(String value) {
		msDXSDKHome = value;
		IPreferenceStore store = MSVCPlugin.getDefault().getPreferenceStore();
		store.setValue(DXSDK_HOME, value);
	}

}